package bookshop.controller.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Customer;
import bookshop.domain.ReceiveAddr;
import bookshop.service.ReceiveAddrService;
import bookshop.service.impl.ReceiveAddrServiceImpl;

public class ReceiverServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String act=request.getParameter("act");
		
		if("input_receiver".equals(act)){
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			String custId=customer.getCustomerId();
			ReceiveAddrService raService = new ReceiveAddrServiceImpl();
			
			String receiver=request.getParameter("receiver");
			String receiverAddr=request.getParameter("receiverAddr");
			String receiverPhone=request.getParameter("receiverPhone");
			String zipCode=request.getParameter("zipCode");
			 
			ReceiveAddr ra=new ReceiveAddr();
			ra.setCustId(custId);
			ra.setReceiver(receiver);
			ra.setReceiverAddr(receiverAddr);
			ra.setReceiverPhone(receiverPhone);
			ra.setZipCode(Integer.parseInt(zipCode));
			ra.setReceiverStatus("Y");
			raService.SavaReceiveAddr(ra);
			response.sendRedirect("order?act=setOrder");
		}
		else if("new_receiver".equals(act)){
			request.getRequestDispatcher("jsps/front/order/add_receive.jsp").forward(request, response);
		}
		else if("add_receiver".equals(act)){
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			String custId=customer.getCustomerId();
			ReceiveAddrService raService = new ReceiveAddrServiceImpl();
			
			String receiver=request.getParameter("receiver");
			String receiverAddr=request.getParameter("receiverAddr");
			String receiverPhone=request.getParameter("receiverPhone");
			String zipCode=request.getParameter("zipCode");
			 
			ReceiveAddr ra=new ReceiveAddr();
			ra.setCustId(custId);
			ra.setReceiver(receiver);
			ra.setReceiverAddr(receiverAddr);
			ra.setReceiverPhone(receiverPhone);
			ra.setZipCode(Integer.parseInt(zipCode));
			ra.setReceiverStatus("Y");
			raService.SavaReceiveAddr(ra);
			response.sendRedirect("order?act=setOrder");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
